﻿using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace WxLib.Pay
{
    /// <summary>
    /// 微信支付统一下单响应
    /// </summary>
    [Serializable]
    [XmlRoot("xml", IsNullable = false)]
    public class UnifiedOrderResponse
    {
        /// <summary>
        /// SUCCESS/FAIL
        ///此字段是通信标识，非交易标识，交易是否成功需要查看result_code来判断
        /// </summary>
        public string return_code { get; set; }
        /// <summary>
        /// 返回信息，如非空，为错误原因
        ///签名失败
        ///参数格式校验错误
        /// </summary>
        public string return_msg { get; set; }

        /*以下字段在return_code为SUCCESS的时候有返回*/

        /// <summary>
        /// 调用接口提交的公众账号ID
        /// </summary>
        public string appid { get; set; }
        /// <summary>
        /// 调用接口提交的店面号
        /// </summary>
        public string mch_id { get; set; }
        /// <summary>
        /// 调用接口提交的终端设备号
        /// </summary>
        public string device_info { get; set; }
        /// <summary>
        /// 微信返回的随机字符串
        /// </summary>
        public string nonce_str { get; set; }
        /// <summary>
        /// 微信返回的签名
        /// </summary>
        public string sign { get; set; }
        /// <summary>
        /// SUCCESS/FAIL
        /// </summary>
        public string result_code { get; set; }
        /// <summary>
        /// 详细参见第6节错误列表
        /// </summary>
        public string err_code { get; set; }
        /// <summary>
        /// 错误返回的信息描述
        /// </summary>
        public string err_code_des { get; set; }

        /*以下字段在return_code 和result_code都为SUCCESS的时候有返回*/

        /// <summary>
        /// 调用接口提交的交易类型，取值如下：JSAPI，NATIVE，APP，详细说明见https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=4_2
        /// </summary>
        public string trade_type { get; set; }
        /// <summary>
        /// 微信生成的预支付会话标识，用于后续接口调用中使用，该值有效期为2小时
        /// </summary>
        public string prepay_id { get; set; }
        /// <summary>
        /// trade_type为NATIVE是有返回，可将该参数值生成二维码展示出来进行扫码支付
        /// </summary>
        public string code_url { get; set; }

        /// <summary>
        /// h5支付返回得url
        /// </summary>
        public string mweb_url { get; set; }

        /// <summary>
        /// 是否下单成功
        /// </summary>
        /// <returns></returns>
        public bool IsSuccess()
        {
            return return_code == "SUCCESS" && result_code == "SUCCESS";
        }
    }
}
